using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame.CollectibleComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CollectibleSpawnerComponent : Microsoft.Xna.Framework.GameComponent
    {
        CollectibleComponent collectible;
        List<CollectibleComponent> collectibleList;

        MapComponent map;
        Game game;

        Texture2D tBattery1, tPaperBag1;
        List<MapComponent.Bounds> boundList;

        public CollectibleSpawnerComponent(Game game, MapComponent map)
            : base(game)
        {
            this.game = game;
            this.map = map;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            collectibleList = new List<CollectibleComponent>();

            base.Initialize();
        }

        public void LoadContent(ContentManager Content)
        {
            tBattery1 = Content.Load<Texture2D>("GFX\\Collectibles\\Battery1");
            tPaperBag1 = Content.Load<Texture2D>("GFX\\Collectibles\\PaperBag1");

            //Get map bounds. Use this in load content to get bounds after map is loaded
            boundList = map.getBounds();
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Camera2DComponent camera)
        {
            //Draw collectibles
            for (int i = 0; i < collectibleList.Count; i++)
                collectibleList[i].Draw(gameTime, spriteBatch, camera);
        }


        public void Spawn(CollectibleComponent.CollectibleType type, Vector2 position, float speedMultiplier)
        {
            CollectibleComponent newCollectible;
            newCollectible = new CollectibleComponent(game, new Vector2(32, 32), position, type, map, speedMultiplier, tPaperBag1, tBattery1, boundList);
            //newCollectible.LoadContent(Content);
            newCollectible.Initialize();

            collectibleList.Add(newCollectible);
        }

        public void Despawn(int listEntry)
        {
            collectibleList.RemoveAt(listEntry);
        }

        public List<CollectibleComponent> GetCollectibleList() { return collectibleList; }

        public void SpawnFromBounds()
        {
            //Start by clearing list
            collectibleList = new List<CollectibleComponent>();

            //Get bounds and add to list
            boundList = map.getBounds();
            for (int i = 0; i < boundList.Count; i++)
            {
                if (boundList[i].type == MapComponent.BoundType.Collectible)
                    Spawn(CollectibleComponent.CollectibleType.Battery, new Vector2(boundList[i].rect.X, boundList[i].rect.Y), 0);
            }
        }
    }
}
